<?php
namespace gzapi\department\controller;

use gzapi\common\business\Department;
use cmf\controller\PublicController;
use think\Validate;

use gzapi\department\controller\ServiceController;
use gzapi\department\controller\ServiceWorkController;

class CompanyController extends PublicController
{

    //方法验证参数
    protected $auth = '*';
    protected $DepartmentCompany;
    protected $gczt = ['5'=>'运行','6'=>'未开工','7'=>'安装'];
    protected $apigczt = ['0'=>'6','1'=>'7','2'=>'5','3'=>'5'];
    protected function initialize()
    {
        if (empty($this->user)) {
            $this->error(['code' => 10001, 'msg' => '登录已失效!']);
        }
        $this->DepartmentCompany = new Department;
        $this->ServiceController = new ServiceController;
        $this->ServiceWorkController = new ServiceWorkController;
    }

    //获取未开工和安装中的工程数量
    public function projectNum()
    {
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->projectNum();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->projectNum();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->projectNum();
            }

        }
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
           if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        $where['fwfgs'] = ['=',$this->user['bm_code']];
        $where['gczt'] = 6;
        //获取未开工数量
        $data['wkg'] = $this->DepartmentCompany->azdNum($where);
        $where['gczt'] = 7;
        //获取安装数量
        $data['az'] = $this->DepartmentCompany->azdNum($where);
        $this->success("查询成功!", ['data' => $data]);
    }
  //查询单个项目信息
    public function projectInfo(){
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->projectInfo();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->projectInfo();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->projectInfo();
            }

        }
        $where = input('param.');
        $res=Db::table('cmf_gz_hise_service_project')->where($where)->find();
        $this->success("查询成功!", ['data' => $res]);
    }
    //查看工程列表
    public function projectList()
    {
        $level = input('param.level');
        if($level == 200)
        {
            
            if($this->user['post'] == 1)
            {
                $this->ServiceController->projectList();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->projectList();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->projectList();
            }
        }
        $type = input('param.type');
        $where = [];
        if(isset($type))
        {
            $where['gczt'] = $this->apigczt[$type];
        }
        $page = input('param.page');
        $limit = input('param.limit');
        $where['fwfgs'] = ['=',$this->user['bm_code']];
        $selectdata = $this->request->param();
        $selectdata['mobilphone'] = $this->user['mobilphone'];
        $selectazd = $this->DepartmentCompany->selectazd($selectdata);
        $where = array_merge($where,$selectazd);
        $indata = $selectdata;
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        //获取关注状态
        $followWhere['bm_code'] = ['=',$this->user['bm_code']];
        $followWhere['mobilphone'] = ['=',$this->user['mobilphone']];
        //获取本状态安装单
        $field = 'jxsxsy,pid,kqshzt,create_time,azd_id,azd_name,provice,city,qx,address,pname';
        $where['xj_fwgcs'] = ['=',$this->user['mobilphone']];
        $data = $this->DepartmentCompany->projectList($where,$field,$page,$limit,[],$followWhere);
        $this->success("查询成功!", ['data' => $data]);
    }

    //获取项目详情
    public function project()
    {
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->project();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->project();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->project();
            }

        }
        $azd_id = input('param.azd_id');
        $where['azd_id'] = $azd_id;
        $data = $this->DepartmentCompany->project($where);
        if($data)
        {
            $this->success("查询成功!", ['data' => $data]);
        }else{
            $this->error('没有数据');
        }
    }

    //获取关注列表数量
    public function followListNum()
    {
        $level = input('param.level');

        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->followListNum();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->followListNum();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->followListNum();
            }

        }
        $data = input('param.');
        if(isset($data['is_strategic']))
        {
            $followWhere['is_strategic'] = $data['is_strategic'];
        }
        $followWhere['bm_code'] = ['=',$this->user['bm_code']];
        $followWhere['mobilphone'] = ['=',$this->user['mobilphone']];
        //获取关注数量
        $azd_ids = $this->DepartmentCompany->followCount($followWhere);
        $this->success("查询成功!", ['data' => $azd_ids['num']]);
    }


    //进入关注列表
    public function followList()
    {
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->followList();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->followList();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->followList();
            }

        }
        $type = input('param.type');
        $follow = 1;
        $where = [];
        if($type)
        {
            $where['gczt'] = $this->apigczt[$type];
        }
        $page = input('param.page');
        $limit = input('param.limit');
        $where['fwfgs'] = ['=',$this->user['bm_code']];
        $selectdata = $this->request->param();
        $selectazd = $this->DepartmentCompany->selectazd($selectdata);
        $where = array_merge($where,$selectazd);
        $followWhere['bm_code'] = ['=',$this->user['bm_code']];
        $followWhere['mobilphone'] = ['=',$this->user['mobilphone']];
        //获取关注的安装单
        $azd_ids = $this->DepartmentCompany->followList($followWhere);
        $where['azd_id'] = ['in' , $azd_ids['follow']];
        //获取本状态关注安装但
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        $where['xj_fwgcs'] = ['=',$this->user['mobilphone']];
        $data = $this->DepartmentCompany->projectList($where,"*",$page,$limit);
        $this->success("查询成功!", ['data' => $data]);
    }

    //获取未完成的巡检安装单数量
    public function inspectListNumIng()
    {
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->inspectListNumIng();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->inspectListNumIng();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->inspectListNumIng();
            }

        }



        $where['a.xj_fwgcs'] = ['=',$this->user['mobilphone']];
        $where['a.xj_xjzt'] = ['in','0,1,2,3,4'];
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'] == 1)
            {
                $config=config('Gzflag');
                $where['w.ywbk'] = ['in',$config];
            }else{
                $config=config('Gzflag');
                $where['w.ywbk'] = ['not in',$config];
            }

            
        }
        $data = $this->DepartmentCompany->inspectListNumIng($where);
        $this->success("查询成功!", ['data' => $data]);
    }

    //进入未完成的巡检列表
    public function inspectList()
    {
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->inspectList();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->inspectList();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->inspectList();
            }

        }
        $inswhere['xj_xjzt'] = ['in','0,1,2,3,4'];
        $inswhere['xj_fwgcs'] = ['=',$this->user['mobilphone']];
        $inspect = $this->DepartmentCompany->inspectListstr($inswhere);
        $type = input('param.type');
        $where = [];
        if($type)
        {
            $where['gczt'] = $this->apigczt[$type];
        }
        $page = input('param.page');
        $limit = input('param.limit');
        $where['fwfgs'] = ['=',$this->user['bm_code']];
        $where['azd_id'] = ['in',$inspect];
        $selectdata = $this->request->param();
        $selectazd = $this->DepartmentCompany->selectazd($selectdata);
        $where = array_merge($where,$selectazd);
        //获取本状态安装单
        $indata = input('param.');
        if(isset($indata['is_strategic']))
        {
            if($indata['is_strategic'])
            {
                $config=config('Gzflag');
                $where['ywbk'] = ['in',$config];
            }else{
                //$where['gclx'] = 2;
                $config=config('Gzflag');
                $where['ywbk'] = ['not in',$config];
            }
        }
        $where['xj_fwgcs'] = ['=',$this->user['mobilphone']];
        $data = $this->DepartmentCompany->projectList($where,"*",$page,$limit);
        $this->success("查询成功!", ['data' => $data]);
    }

    //取消关注
    public function followOff()
    {
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->followOff();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->followOff();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->followOff();
            }

        }
        $azd_id = input('param.azd_id');
        $where = [];
        $where['azd_id'] = ['=',$azd_id];
        $where['bm_code'] = ['=',$this->user['bm_code']];
        $where['mobilphone'] = ['=',$this->user['mobilphone']];
        $result = $this->DepartmentCompany->followOffOn(2,$where);
        if($result)
        {
            $this->success("取消关注成功!");
        }else{
            $this->error('取消关注失败');
        }
    }

    //点击关注
    public function followOn()
    {
        $level = input('param.level');
        if($level == 200)
        {
            if($this->user['post'] == 1)
            {
                $this->ServiceController->followOn();
            }
            if($this->user['post'] == 2)
            {
                $this->ServiceWorkController->followOn();
            }
            if($this->user['post'] == '')
            {
                $this->ServiceWorkController->followOn();
            }

        }
        $azd_id = input('param.azd_id');
        $where = [];
        $where['azd_id'] = ['=',$azd_id];
        $where['bm_code'] = ['=',$this->user['bm_code']];
        $where['mobilphone'] = ['=',$this->user['mobilphone']];
        $data['azd_id'] = $azd_id;
        $data['bm_code'] = $this->user['bm_code'];
        $data['mobilphone'] = $this->user['mobilphone'];
        $result = $this->DepartmentCompany->followOffOn(1,$where,$data);
        if($result)
        {
            $this->success("添加关注成功!");
        }else{
            $this->error('添加关注失败');
        }
    }

    //获取分公司
    public function getfwgcs()
    {
        $where['bm_code'] = $this->user['bm_code'];
        $where['gw'] = 5;
        $data = $this->DepartmentCompany->getfwgcs($where);
        $this->success('查询成功',$data);
    }

    //修改项目信息
    public function editazd()
    {
        $data = input('param.');
        $updata = [];
        if(isset($data['pname']) && $data['pname']){
            $updata['pname'] = $data['pname'];
        }
        if(isset($data['azd_name']) && $data['azd_name']){
            $updata['azd_name'] = $data['azd_name'];
        }
        if(isset($data['address']) && $data['address']){
            $updata['address'] = $data['address'];
        }
        if(isset($data['first_party_contacts']) && $data['first_party_contacts']){
            $updata['first_party_contacts'] = $data['first_party_contacts'];
        }
        if(isset($data['first_party_phone']) && $data['first_party_phone']){
            $updata['first_party_phone'] = $data['first_party_phone'];
        }
        if(isset($data['installer_contacts']) && $data['installer_contacts']){
            $updata['installer_contacts'] = $data['installer_contacts'];
        }
        if(isset($data['installer_phone']) && $data['installer_phone']){
            $updata['installer_phone'] = $data['installer_phone'];
        }
        if(isset($data['service_engineer']) && $data['service_engineer']){
            $updata['service_engineer'] = $data['service_engineer'];
        }
        if(isset($data['service_engineer_id']) && $data['service_engineer_id']){
            $updata['service_engineer_id'] = $data['service_engineer_id'];
        }
        if(isset($data['vip_type']) && $data['vip_type']){
            $updata['vip_type'] = $data['vip_type'];
        }
        if(isset($data['chain_type']) && $data['chain_type']){
            $updata['chain_type'] = $data['chain_type'];
        }
        if(!$updata)
        {
            $this->error('参数错误');

        }
        if(isset($data['azd_id']) && $data['azd_id']){
            $where['azd_id'] = $data['azd_id'];
        }else{
            $this->error('条件错误');
        }
        $res = $this->DepartmentCompany->editazd($where,$updata);
        $this->success("修改成功!");
    }











}
